package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.dongdongsongcan.app.DDApp;
import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.adapter.AreaAdapter;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.SearchControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AddressData.Address;
import com.dongdongsongcan.app.control.bean.AreaData;
import com.dongdongsongcan.app.control.bean.DDLocation;
import com.dongdongsongcan.app.utils.LocationUtil;
import com.dongdongsongcan.app.view.AddressItem;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

import java.util.List;

public class AreaChooseActivity extends BaseActivity {
    private EditText mChoosearea_et;
    private ListView mSearch_list;
    private AreaAdapter mAreaAdapter;
    private Button mChoosearea_btn_clear;
    private static AddressEditActivity mAddressEditActivity;

    public static void startFrom(Context context) {
        DpActivityManager.i().startActivity(context,
                AreaChooseActivity.class);
        if (context instanceof AddressEditActivity) {
            mAddressEditActivity = (AddressEditActivity) context;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_choose);
        getDpTitleBar().setTitle("选择送货区域");

    }

    @Override
    public void initView() {
        mChoosearea_et = (EditText) findViewById(R.id.choosearea_et);
        mSearch_list = (ListView) findViewById(R.id.search_list);
        mChoosearea_btn_clear = (Button) findViewById(R.id.choosearea_btn_clear);
        mAreaAdapter = new AreaAdapter(this);
        mSearch_list.setAdapter(mAreaAdapter);
    }

    @Override
    public void setListener() {

        mSearch_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaData.Area area = mAreaAdapter.getData(position);
                mAddressEditActivity.setArea(area);
                mAddressEditActivity = null;
                AreaChooseActivity.this.finish();
            }
        });

        mChoosearea_btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChoosearea_et.setText("");
            }
        });
        mChoosearea_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    mSearch_list.setVisibility(View.INVISIBLE);
                } else {
                    mSearch_list.setVisibility(View.VISIBLE);
                    String keyWord = s.toString();
                    if (TextUtils.isEmpty(keyWord)) return;
                    SearchControl.search(keyWord, new IControlResponse<AreaData>() {
                        @Override
                        public void onControlResponse(AreaData data) {
                            if (data.isOk()) {
                                List<AreaData.Area> areas = data.getResult();
                                mAreaAdapter.setData(areas);
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void initData() {

    }

}
